package business.optionForm_Evt;

import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;

import presentation.optionForm_UI.ChangeKeyWindow;
import service.Service;

public class Controls_Evt {

        private Service ser = new Service();
        JDialog dlg;
        private JButton[] listButton;
        public String[] listArrows = new String[]{"↑", "↓", "←", "→"};
        public String[] defaultKeys = new String[]{"↑", "↓", "←", "→", "Space","Enter","BackSpace"};

        public Controls_Evt(JDialog dlg) {
                this.dlg = dlg;
        }
        
        public void setListButton(JButton[] listButton)
        {
        	this.listButton = listButton;
        }
        
        public void btCancel_Evt() {
                dlg.setVisible(false);
        }

        public void btSave_Evt() {
        	
        }

        public void btSpace_Evt() {
        }

        public void btEnter_Evt() {
        }
        
        public void changeKeyEvent(JButton button)
        {
        	new ChangeKeyWindow(dlg, button, this.listButton).setVisible(true);
        }
        
        public int convertKey(String text)
        {
        	if(text.toLowerCase().equals("space"))
        		return KeyEvent.VK_SPACE;
        	if(text.toLowerCase().equals("enter"))
        		return KeyEvent.VK_ENTER;
        	if(text.toLowerCase().equals("backspace"))
        		return KeyEvent.VK_BACK_SPACE;
        	return text.codePointAt(0);
        }
        
        /**
         * This method will set key text to your button and also it will return
         * a list of key that store in file
         *
         * @param btUp
         * @param btDown
         * @param btLeft
         * @param btRight
         * @param btSelect
         * @param btSubmit
         * @param btClear
         * @return
         */
        public int[] loadCustomControlKeys(JButton btUp, JButton btDown, JButton btLeft, JButton btRight, JButton btSelect, JButton btSubmit, JButton btClear) {
                int[] listControlKeys = ser.gameControls();
                JButton[] list = new JButton[]{btUp, btDown, btLeft, btRight, btSelect, btSubmit, btClear};
                for (int i = 0; i < listControlKeys.length; i++) {
                        switch (listControlKeys[i]) {
                                case 38: case 8593:
                                        list[i].setText(listArrows[0]);
                                        break;
                                case 40: case 8595:
                                        list[i].setText(listArrows[1]);
                                        break;
                                case 37: case 8592:
                                        list[i].setText(listArrows[2]);
                                        break;
                                case 39: case 8594:
                                        list[i].setText(listArrows[3]);
                                        break;
                                case KeyEvent.VK_ENTER:
                                        list[i].setText("Enter");
                                        break;
                                case KeyEvent.VK_SPACE:
                                        list[i].setText("Space");
                                        break;
                                case KeyEvent.VK_BACK_SPACE:
                                        list[i].setText("BackSpace");
                                        break;
                                default:
                                        list[i].setText((char) listControlKeys[i] + "");
                                        break;
                        }
                }
                return listControlKeys;
        }

        public void updateControlKeys(int up, int down, int left, int right, int select, int submit, int clear) {
                ser.updateGameControls(up, down, left, right, select, submit, clear);
        }
        
}
